package com.sks.web.action.privilege;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.pojo.Users;
import com.sks.service.privilege.UsersService;
import com.sks.utils.StringUtil;
import com.sks.web.formbean.privilege.UsersForm;
/**
 * 员工分页列表
 */
@Controller("/cms/privilege/users/list")
public class UsersListAction extends Action {
	@Resource UsersService usersService;

	@Override @Permission(module="users", privilege="view")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		PageView<Users> pageView = new PageView<Users>(12, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("registerTime", "desc");
		if("true".equals(formbean.getQuery())){//如果来自查询页面
			StringBuilder jpql = new StringBuilder();
			List<Object> params = new ArrayList<Object>();
			/*if(formbean.getUserName()!=null && !"".equals(formbean.getUserName().trim())){
				params.add("%"+ formbean.getUserName().trim() +"%");
				jpql.append("o.userName like ?").append(params.size());
			}*/
			if(formbean.getLoginName()!=null && !"".equals(formbean.getLoginName().trim())){
				if(!params.isEmpty()) jpql.append(" and ");
				params.add("%"+ formbean.getLoginName().trim() +"%");
				jpql.append("o.loginName like ?").append(params.size());
			}
			/*if(formbean.getEmail()!=null && !"".equals(formbean.getEmail().trim())){
				if(!params.isEmpty()) jpql.append(" and ");
				params.add(formbean.getEmail());
				jpql.append("o.email=?").append(params.size());
			}*/
			pageView.setQueryResult(
					usersService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),
							jpql.toString(), params.toArray(), orderby));
		}else{
			pageView.setQueryResult(usersService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
		}	
		
		request.setAttribute("pageView", pageView);
		
		String isSelect = (String) request.getParameter("isSelect");
		if (!StringUtil.isEmpty(isSelect) && "true".equalsIgnoreCase(isSelect)) {
			return mapping.findForward("listBySelect");
		}
		return mapping.findForward("list");
	}

}
